#
# Cookbook Name:: om
# Recipe:: install
#
# Copyright 2013, IBM
#
# All rights reserved - Do Not Redistribute
#
startinstr "Install OM application"

include_recipe "gitclientsetup::default"

wasuser = node[:was][:user]
wasgroup = node[:was][:group]
cuser = node[:chef][:user]
cgroup = node[:chef][:group]
deployment = node[:deployment]
dbhost=""

inst_temp = node[:common][:inst_temp]+"/om/"

search(:node, "deployment:#{deployment}").each do |anode|
	if anode.role?("om_runtdb2")
		dbhost = anode[:fqdn]
	end
end
jmshost=""
search(:node, "deployment:#{deployment}").each do |anode|
    if anode.role?("om_mq")
        jmshost = anode[:fqdn]
    end
end
group "#{node[:was][:group]}" do
  action :create
end

user "#{node[:was][:user]}" do
  gid "#{node[:was][:group]}"
  action :create
end

directory "#{node[:om][:installParentDir]}" do
  owner wasuser
  group wasgroup
  action :create
end 

wasuserpw = node[:was][:userpw]
bash "changepassword for non-root user" do
  user "root"
  code <<-EOH
    usermod -a -G users #{node[:was][:user]}
    echo "#{node[:was][:user]}:#{node[:was][:userpw]}" | /usr/sbin/chpasswd
  EOH
end


#creating directory that will hold the migration shell script
directory "/migration" do
  user cuser
  group cgroup
  action :create
end

#creating the migration shell script from template
template "/migration/agent.sh" do
  source "agent.sh.erb"
  backup false
  owner cuser
  group cgroup
  mode 0755
  variables(
		:jmshost=>jmshost,
		:dbhost=>dbhost,
		:installdir=> "#{node[:om][:installParentDir]}"
  )
  action :create
end

#creating the migration shell script from template
template "/migration/intg.sh" do
  source "intg.sh.erb"
  backup false
  owner cuser
  group cgroup
  mode 0755
  variables(
    :jmshost=>jmshost,
    :dbhost=>dbhost,
    :installdir=> "#{node[:om][:installParentDir]}"
  )
  action :create
end

#copying the ri data loader xml to /tmp folder
cookbook_file "/tmp/ri_loader_themestore.xml" do
  backup false
  owner wasuser
  group wasgroup
  mode 0755
  source "ri_loader_themestore.xml"
  action :create
end

include_recipe "om::customer_overrides"
include_recipe "om::runt_installsilent"
include_recipe "om::agentservice"
include_recipe "om::intgservice"
include_recipe "om::runt_multischemaxml"
include_recipe "java7::install"

include_recipe "om::download_setup"
#include_recipe "om::download_sample"

deployment = node[:deployment]
installWCC = node[:common][:installWCC]
class Chef::Recipe
include Orchestrate
end

zkep = "#{node[:zookeeper][:ip]}:#{node[:zookeeper][:port]}"
chef_gem 'zk'

# Copy is done so that the installing user will have the right permissions on the files?.
bash "Copy OM setup files" do
	user wasuser
	group wasgroup
	cwd inst_temp
	creates "#{node[:om][:installParentDir]}/setupcopy.complete"
	code <<-EOH
		cp -r #{inst_temp}/setup #{node[:om][:installParentDir]} && touch #{node[:om][:installParentDir]}/setupcopy.complete
	EOH
end


#bash "Copy OM sample files" do
#  	user wasuser
#  	group wasgroup
#  	cwd "/tmp/om"
#  	creates "#{node[:om][:installParentDir]}/samplecopy.complete"
#  	code <<-EOH
#  		cp -r /tmp/om/sample #{node[:om][:installParentDir]} 
#			if [ $? == 0 ]; then
#				touch #{node[:om][:installParentDir]}/samplecopy.complete
#			fi
#  	EOH
#end

ruby_block "Wait for database status to be up before trying to install." do
    block do
		i_url = "/deployments/" + deployment + "/config/om/runtime/db2/status"
        dep = Dependency.new(i_url, node[:client][:status], zkep)
        dep.validate!
    end
end

bash "Excecute silent install for base OMS" do
	creates "#{node[:om][:installDir]}/InstallSI.log" 
	user wasuser
	group wasgroup
	cwd "#{inst_temp}/setup"
	timeout 36000
	code <<-EOH
		echo "Executing cmd - #{node[:java7][:installDir]}/bin/java -jar SMCFS_9.4.jar -f /tmp/install.silent"
		"#{node[:java7][:installDir]}/bin/java" -jar PRICE_9.4.jar -f "/tmp/install.silent"			
	EOH
end

#include_recipe "om::install_ri"

installs = [
	{:name => "com.ibm.mq.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0" , :targetjvm => "EVERY" },
	{:name => "com.ibm.mqjms.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0" , :targetjvm => "EVERY"},
	{:name => "com.ibm.mq.jmqi.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0" , :targetjvm => "EVERY"},
	{:name => "com.ibm.mq.headers.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0" , :targetjvm => "EVERY"},	
	{:name => "connector.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0", :targetjvm => "EVERY" },
	{:name => "dhbcore.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0", :targetjvm => "EVERY" },
	{:name => "fscontext.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0", :targetjvm => "EVERY" },
	{:name => "jms.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0", :targetjvm => "EVERY" },
	{:name => "jndi.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0", :targetjvm => "EVERY" },
	{:name => "jta.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0", :targetjvm => "EVERY" },
	{:name => "providerutil.jar", :type => "thirdparty", :vendor => "mq", :version => "7_0", :targetjvm => "EVERY" },
	{:name => "ant-contrib.jar", :type => "thirdparty", :vendor => "ant" , :version => "1_8_1", :targetjvm => "EVERY" },
	{:name => "com.ibm.ws.ejb.thinclient_7.0.0.jar", :type => "thirdparty", :vendor => "defaultjms" , :version => "7_0", :targetjvm => "AGENT" },
	{:name => "com.ibm.ws.sib.client.thin.jms_7.0.0.jar", :type => "thirdparty", :vendor => "defaultjms" , :version => "7_0", :targetjvm => "AGENT" },
	{:name => "com.ibm.ws.wccm.jar", :type => "thirdparty", :vendor => "defaultjms" , :version => "7_0", :targetjvm => "AGENT" },
	{:name => "com.ibm.ws.security.crypto.jar", :type => "thirdparty", :vendor => "defaultjms" , :version => "7_0", :targetjvm => "AGENT" }
] 

if (installWCC == "true" || installWCC == true)
	installs.push({:name => "ISCCS.jar", :type => "service"})
end

installs.each do |install| 
	if install[:type] == "service" 
		bash "Execute installService for #{install[:name]}"  do
			user wasuser
			group wasgroup
			cwd node[:om][:installDir]
			creates "#{node[:om]['installDir']}/.installcomplete/#{install[:name]}.complete"
			code <<-EOH
					#{node[:om]['installDir']}/bin/InstallService.sh #{inst_temp}/setup/#{install[:name]}
					if [ $? == 0 ]; then
						mkdir -p #{node[:om]['installDir']}/.installcomplete/
						touch #{node[:om][:installDir]}/.installcomplete/#{install[:name]}.complete
					fi
			EOH
		end
	else
		bash "Execute install3rdParty for #{install[:name]}"  do
			user wasuser
			group wasgroup
			cwd node[:om][:installDir]
			creates "#{node[:om][:installDir]}/jar/#{install[:vendor]}/#{install[:version]}/#{install[:name]}"
			code <<-EOH
					set -x
					#{node[:om][:installDir]}/bin/install3rdParty.sh #{install[:vendor]} #{install[:version]} -j #{inst_temp}/setup/thirdpartyjars/#{install[:name]} -targetJVM #{install[:targetjvm]}
					set +x
			EOH
		end
	end
end
#include_recipe "om::build_wars"

bash "Rebuild resource.jar"  do
  user wasuser
  group wasgroup
  cwd node[:om][:installDir]
  code <<-EOH
    #{node[:om][:installDir]}/bin/deployer.sh -t resourcejar
  EOH
end
  
bash "Copy customer overrides"  do
    user node[:was][:user]
    group node[:was][:group]
    code <<-EOH
      cp /tmp/customer_overrides.properties #{node[:om][:yfs_local]}/properties
    EOH
end

include_recipe "om::deployment_accelerator" if (node[:dacconfig]!=nil && node[:dacconfig].empty? != true)

include_recipe "om::runt_buildear"
include_recipe "om::fetchfilebindings"

template "/tmp/clean-installers.sh" do
  source "clean-installers.sh.erb"
  backup false
  owner "root"
  group "root"
  mode 0755
  variables(
        :tmpdir=>inst_temp
  )
  action :create
end

#install and configure logstash for agent server after runtime install.
include_recipe "logstash::config_for_agent"

stopinstr "Install OM application"
